<?php
/**
 * 报价单
 */
quanli_require('module/base/bill/billSale.class.php');
quanli_require('module/base/system/sysDepartment.class.php');
quanli_require('module/base/system/sysUser.class.php');

class pa_billSale {

    public function execute()
    {
        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0)
        {
            switch($_REQUEST['operFlg'])
            {
                case 1: //订单列表
                    $this->lists();
                    break;

                case 2: //保存订单
                    $this->save();
                    break;

                case 3: //订单列表-生产
                    $this->lists(1);
                    break;

                default:
                    $this->toError('参数错误');
                    break;
            }
        } else {
            $this->toError('参数错误');
        }
    }

    /**
     * 转跳到错误页
     * @param string $msg 错误信息
     */
    private function toError( $msg )
    {
        global $G_PGLOB;
        $G_PGLOB->display_error_page( $msg );
    }

    /**
     * 列表
     */
    private function lists($type = 0)
    {
        global $G_PGLOB;
        //实例用户类
        $sysUser = new sysUser( $G_PGLOB->DBCA );

        //当前登录用户信息
        $args_array = array(
            'userID' => $G_PGLOB->currentUserID
        );
        $userInfo_array = $sysUser->searchUserInfo( $args_array );
        $departmentID = $userInfo_array[0]['departmentID'];

        //实例化部门类
        $sysDepartment = new sysDepartment();

        //部门数据
        $department_array = array();
        //递归查询部门数据
        $sysDepartment->searchDepartment( $department_array, 0, -1,$departmentID);

        //生成部门下拉框html代码
        $department_select_html = '';
        $sysDepartment->createDepartmentSelectOptionHtml( $department_array, $department_select_html );
        $G_PGLOB->smarty->assign_by_ref('department_select_html', $department_select_html);

        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(60000));

        $G_PGLOB->smarty->assign('type', $type);
        $G_PGLOB->smarty->display('bill/billSale.tpl');
    }

    /**
     * 保存
     */
    private function save()
    {
        global $G_PGLOB;

        $params = array('useFlg'=>1);
        //分类
        quanli_include('module/base/goods/goodsCategory.class.php');
        $categoryHandler = new goodsCategory( $G_PGLOB->DBCA );
        $category = $categoryHandler->searchBigCategory( $params );
        $G_PGLOB->smarty->assign('category', json_encode($category));

        //物流
        quanli_require('module/base/system/sysSend.class.php');
        $sysSend = new sysSend();
        $send_array = $sysSend->searchSendType( $params );
        $G_PGLOB->smarty->assign('send_array', $send_array);

        $saleInfo = null;
        $currDate = date('Y-m-d');

        if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])){
            $billSale = new billSale( $G_PGLOB->DBCA );
            $param = array( 'saleCode' => $_REQUEST['code'] );
            $result = $billSale->searchList($param);
            if ($result) {
                $saleInfo = $result[0];
                $currDate = date('Y-m-d', strtotime($saleInfo->createTime));
            }
            $saleInfo->dtl = $billSale->searchDtl($_REQUEST['code']);

            $temp_annex = null;
            $resAnnex = $billSale->searchAnnex($_REQUEST['code']);
            if($resAnnex){
                foreach ($resAnnex as $annex){
                    $annex->path = G_IMAGE_URL.'goods/wx/'.$annex->fileName;
                    $temp_annex[$annex->categoryName.'-'.$annex->goodsName][] = $annex;
                }
            }
            $saleInfo->annex = $temp_annex;
            $saleInfo->send = $billSale->saleSend($_REQUEST['code']);
        }
        $type = isset($_REQUEST['type'],$_REQUEST['code']) ? $_REQUEST['type']:0;
        $G_PGLOB->smarty->assign('type', $type);
        $G_PGLOB->smarty->assign('saleInfo', $saleInfo);
        $G_PGLOB->smarty->assign('currDate', $currDate);
        $G_PGLOB->smarty->display('bill/billSaleAdd.tpl');
    }

}